<html>
<head><meta charset="utf-8"><title>repr(C) unions need padding · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html">repr(C) unions need padding</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="169330741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169330741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169330741">(Jun 30 2019 at 08:22)</a>:</h4>
<p>This <code>union</code> needs to have padding:</p>
<div class="codehilite"><pre><span></span><span class="cp">#[repr(C)]</span><span class="w"> </span><span class="k">struct</span> <span class="nc">S</span><span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u32</span><span class="p">);</span><span class="w"></span>
<span class="cp">#[repr(C)]</span><span class="w"> </span><span class="k">union</span> <span class="nc">U</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">s</span>: <span class="nc">S</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</pre></div>


<p>When doing a roundtrip from Rust-&gt;C-&gt;Rust, the content of the padding won't be preserved.</p>



<a name="169330791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169330791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169330791">(Jun 30 2019 at 08:23)</a>:</h4>
<p>The "bag of bits" definition is fine, in that one can read and write all bits of the union. But if the bit is a padding bit, reading it returns <code>undef</code>.</p>



<a name="169332304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169332304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169332304">(Jun 30 2019 at 09:17)</a>:</h4>
<p>so that's a thing in C as well?</p>



<a name="169332308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169332308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169332308">(Jun 30 2019 at 09:18)</a>:</h4>
<p>dang :(</p>



<a name="169333180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333180">(Jun 30 2019 at 09:46)</a>:</h4>
<p>@ralf no, i was wrong</p>



<a name="169333182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333182">(Jun 30 2019 at 09:46)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="169333186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333186">(Jun 30 2019 at 09:47)</a>:</h4>
<p>I've posted in the issue</p>



<a name="169333195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333195">(Jun 30 2019 at 09:47)</a>:</h4>
<p>our repr(C) union implementation has a bug if it doesn't preserve the contents of field padding</p>



<a name="169333661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333661">(Jun 30 2019 at 10:02)</a>:</h4>
<p>aha? so C does preserve it?</p>



<a name="169333826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333826">(Jun 30 2019 at 10:08)</a>:</h4>
<p>no, C does not preserve it</p>



<a name="169333835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333835">(Jun 30 2019 at 10:08)</a>:</h4>
<p>C unions might only have trailing padding, and that trailing padding does not need to be preserved</p>



<a name="169333848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333848">(Jun 30 2019 at 10:09)</a>:</h4>
<p>If a union does not have trailing padding, then it has no padding - but union fields can have padding</p>



<a name="169333850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169333850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169333850">(Jun 30 2019 at 10:09)</a>:</h4>
<p>Does that make sense?</p>



<a name="169335074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169335074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169335074">(Jun 30 2019 at 10:50)</a>:</h4>
<p>but it cant know which field it is, in general? I thought C unions are "extensible"</p>



<a name="169335080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169335080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169335080">(Jun 30 2019 at 10:51)</a>:</h4>
<p>as in, if one side has fewer fields than the other, data can still be forwarded correctly</p>



<a name="169335081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28C%29%20unions%20need%20padding/near/169335081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(C).20unions.20need.20padding.html#169335081">(Jun 30 2019 at 10:51)</a>:</h4>
<p>(that's AFAIK something that libraries use to remain forward compatible)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>